#!/bin/bash

# =====================================================
# 删除旧 SQL 文件脚本
# =====================================================
# 用途：删除已被新编号文件替代的旧 SQL 文件
# 使用方法：chmod +x DELETE_OLD_SQL_FILES.sh && ./DELETE_OLD_SQL_FILES.sh
# =====================================================

set -e

# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

info() {
    echo -e "${BLUE}ℹ️  $1${NC}"
}

success() {
    echo -e "${GREEN}✅ $1${NC}"
}

warning() {
    echo -e "${YELLOW}⚠️  $1${NC}"
}

error() {
    echo -e "${RED}❌ $1${NC}"
}

echo ""
echo "========================================"
echo "  删除旧 SQL 文件"
echo "========================================"
echo ""

# 要删除的文件列表
FILES_TO_DELETE=(
    "database-init.sql"
    "database-seed.sql"
    "database-init-complete.sql"
    "database-seed-complete.sql"
    "database-migration-minio.sql"
    "database-migration-add-upload-source.sql"
    "setup-database.sh"
)

# 检查文件是否存在
info "检查要删除的文件..."
EXISTING_FILES=()
for file in "${FILES_TO_DELETE[@]}"; do
    if [ -f "$file" ]; then
        EXISTING_FILES+=("$file")
        echo "  ✓ $file"
    fi
done

if [ ${#EXISTING_FILES[@]} -eq 0 ]; then
    success "没有找到要删除的文件，可能已经删除过了"
    exit 0
fi

echo ""
warning "将要删除以下 ${#EXISTING_FILES[@]} 个文件："
for file in "${EXISTING_FILES[@]}"; do
    echo "  - $file"
done

echo ""
read -p "$(echo -e ${YELLOW}'确认删除？(y/n): '${NC})" -r
echo

if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    warning "操作已取消"
    exit 0
fi

# 删除文件
info "开始删除文件..."
for file in "${EXISTING_FILES[@]}"; do
    if rm "$file" 2>/dev/null; then
        success "已删除: $file"
    else
        error "删除失败: $file"
    fi
done

echo ""
success "删除完成！"
echo ""
info "保留的文件："
echo "  ✓ 00-quick-init.sh"
echo "  ✓ 01-database-init-complete.sql"
echo "  ✓ 02-database-seed-complete.sql"
echo "  ✓ 03-database-migration-minio.sql"
echo "  ✓ 04-database-migration-add-upload-source.sql"
echo "  ✓ clear-database.sql"
echo ""

